/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.vdialog.view.page;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import offset.nodes.Constants;
import offset.nodes.client.chooser.view.TypeChooserDialog;
import offset.nodes.client.model.Initializable;
import offset.nodes.client.model.ActivePanelContainer;
import offset.nodes.client.model.NodeTypes;
import offset.nodes.client.model.Server;
import offset.nodes.client.model.SimpleNamespaceRegistry;
import offset.nodes.client.virtual.model.GetSchemas;
import offset.nodes.client.virtual.model.jcr.QName;
import offset.nodes.client.virtual.model.jcr.nodetype.NodeTypeDefinition;
import offset.nodes.client.virtual.model.jcr.nodetype.NodeTypeReader;

/**
 *
 * @author Walter Lütgenau
 */
public class NewVirtualPagePanel extends javax.swing.JPanel implements Initializable {
    Server model;
    NodeTypes types = null;
    TypeChooserDialog typeChooser = null;

    /** Creates new form NewVirtualPagePanel */
    public NewVirtualPagePanel(Server model) {
        initComponents();
        this.model = model;
    }

    public void init(ActivePanelContainer container) {
        initTypes(model);
    }

    protected void initTypes(Server server) {
        GetSchemas.Request request = new GetSchemas.Request();
        GetSchemas.Response response = null;
        try {
            response = (GetSchemas.Response) server.sendRequest(request);
            InputStream in = new ByteArrayInputStream(response.getSchemaXml().getBytes());
            NodeTypeDefinition[] definitions = NodeTypeReader.read(in);
            types = new NodeTypes(definitions);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * Return the name chosen by the user
     * 
     * @return
     */
    public String getVirtualPageName() {
        return nameTextField.getText();
    }

    /**
     * Return the node type chosen by the user
     * 
     * @return
     */
    public String getNodeType() {
        return nodeTypeTextField.getText();
    }

    /**
     * Shall the new virtual page be directly edited?
     * @return
     */
    public boolean doEdit() {
        return editCheckbox.isSelected();
    }

    /**
     * Can finish be executed?
     * 
     * @return
     */
    public boolean isValidPane() {
        if (nodeTypeTextField.getText() != null && nodeTypeTextField.getText().length() > 0
                && nameTextField.getText() != null && nameTextField.getText().length() > 0)
            return true;
        return false;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        nameLabel = new javax.swing.JLabel();
        nameTextField = new javax.swing.JTextField();
        nodeTypeLabel = new javax.swing.JLabel();
        nodeTypeTextField = new javax.swing.JTextField();
        nodeTypeButton = new javax.swing.JButton();
        editCheckbox = new javax.swing.JCheckBox();

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("offset/nodes/client/vdialog/view/DialogBundle"); // NOI18N
        nameLabel.setText(bundle.getString("virtualPage.name")); // NOI18N

        nodeTypeLabel.setText(bundle.getString("virtualPage.nodeType")); // NOI18N

        nodeTypeButton.setText("...");
        nodeTypeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectType(evt);
            }
        });

        editCheckbox.setSelected(true);
        editCheckbox.setText(bundle.getString("virtualNode.edit")); // NOI18N
        editCheckbox.setMargin(new java.awt.Insets(0, 0, 2, 2));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(nameLabel)
                    .addComponent(nodeTypeLabel))
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(editCheckbox)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(nodeTypeTextField, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(nameTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(nodeTypeButton)))
                .addContainerGap(100, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(76, 76, 76)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameLabel)
                    .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(47, 47, 47)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nodeTypeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(nodeTypeLabel)
                    .addComponent(nodeTypeButton))
                .addGap(42, 42, 42)
                .addComponent(editCheckbox)
                .addContainerGap(114, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void selectType(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectType
        if (typeChooser == null)
            typeChooser = new TypeChooserDialog(null, true,
                    types.getArray());

        typeChooser.setVisible(true);
        if (typeChooser.getReturnStatus() == TypeChooserDialog.RET_OK)
            nodeTypeTextField.setText(SimpleNamespaceRegistry.getInstance().toPrefixName(typeChooser.getDefinition().getName()));
    }//GEN-LAST:event_selectType
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox editCheckbox;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JButton nodeTypeButton;
    private javax.swing.JLabel nodeTypeLabel;
    private javax.swing.JTextField nodeTypeTextField;
    // End of variables declaration//GEN-END:variables
}
